//
//  OpenMemberView.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/2/14.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class OpenMemberView: UIView {
    
    // 常量
    private let tips = ["包月","包季","包年"]
    private let moneys = ["199","499","999"]
    private let color_border = Palette.colorFromRGB(179, green: 136, blue: 81)
    private let color_backround = Palette.colorFromRGB(249, green: 243, blue: 237)
    // 立即开通按钮
    private let saveBtn = UIButton(title: "立即开通", name: "button_orange",radius: 5)
    // 滑动
    private var scrollview = UIScrollView()
    // 记录金额序号
    private var moneyIndex:Int = 0
    // 回调
    public var complete:((_ money:String,_ btntext:String) -> Void)?
    
    
    public var userInfo:UserInfo? {
        didSet {
            updateLayout()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        init_view()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private var lables = [UILabel]()
    
    private func init_view() -> Void {
        (0...9).forEach { (i) in
            lables.append(UILabel())
        }
    }
    
    override func layoutSubviews() {
        guard let user = userInfo else {
            return
        }
        // 头像
        let headerPic = UIImageView(image: UIImage(named: "main_avatar_97x97"))
        headerPic.frame = CGRect(x: MARGIN, y: MARGIN15, width: MARGIN60, height: MARGIN60)
        scrollview.addSubview(headerPic)
        // 姓名
        let usernameX = headerPic.frame.maxX + MARGIN10
        let usernameW = frame.width - usernameX - MARGIN
        let usernameLabel = lables[0]
        usernameLabel.textColor = UIColor.black
        usernameLabel.font = UIFont.systemFont(ofSize: 18)
        usernameLabel.text = user.username
        usernameLabel.frame = CGRect(x: usernameX, y: MARGIN25, width: usernameW, height: MARGIN25)
        scrollview.addSubview(usernameLabel)
        // 会员描述
        let preTimeLabel = lables[1]
        preTimeLabel.textColor = UIColor.gray
        preTimeLabel.font = UIFont.systemFont(ofSize: 13)
        if user.permissionid == 2 {
            switch user.openmoney {
            case "199":
                preTimeLabel.text = "您现在是包月会员"
            case "499":
                preTimeLabel.text = "您现在是包季会员"
            case "999":
                preTimeLabel.text = "您现在是包年会员"
            default:
                break
            }
            saveBtn.setTitle("关闭会员", for: .normal)
        }
        if user.permissionid == 3 {
            preTimeLabel.text = "您的会员已过期，请重新开通"
        }
        if user.permissionid == 1 {
            saveBtn.setTitle("立即开通", for: .normal)
        }
        let preTimeW = frame.width - usernameX - MARGIN10
        preTimeLabel.frame = CGRect(x: usernameX, y: usernameLabel.frame.maxY, width: preTimeW, height: MARGIN25)
        scrollview.addSubview(preTimeLabel)
        // 下划线
        let line = UIView(frame: CGRect(x: 0, y: headerPic.frame.maxY + MARGIN25, width: frame.width, height: MARGIN10))
        line.backgroundColor = COLOR_LIGHT_WHITE
        scrollview.addSubview(line)
        // 特权提示
        let tip = lables[2]
        tip.text = "开通会员畅享三大特权"
        tip.textColor = COLOR_TIP
        tip.font = UIFont.systemFont(ofSize: 18)
        let tipW = frame.width - MARGIN * 2
        tip.frame = CGRect(x: MARGIN, y: line.frame.maxY + MARGIN20, width: tipW, height: MARGIN20)
        scrollview.addSubview(tip)
        // 特权
        let tipContent = lables[3]
        tipContent.textColor = UIColor.gray
        tipContent.font = UIFont.systemFont(ofSize: 13)
        tipContent.numberOfLines = 0
        let str = "每笔订单可享受88折优惠、支持在线选座功能(也可为其他乘机人进行选座)、同时特价机票享受折上折优惠。"
        let style = NSMutableParagraphStyle()
        style.lineSpacing = MARGIN10
        tipContent.attributedText = NSAttributedString(string: str, attributes: [.paragraphStyle : style])
        tipContent.frame = CGRect(x: MARGIN, y: tip.frame.maxY, width: tipW, height: MARGIN90)
        scrollview.addSubview(tipContent)
        // 下划线
        let line2 = UIView(frame: CGRect(x: MARGIN, y: tipContent.frame.maxY, width: tipW, height: 1))
        line2.backgroundColor = COLOR_LINE
        scrollview.addSubview(line2)
        // 记录高度
        var tempHeight:CGFloat = 0
        // 开通金额
        for i in 0..<3 {
            let subMember = SubMemberView()
            subMember.tag = i
            let subW = (frame.width - MARGIN * 4) / 3
            let subX = MARGIN + CGFloat(i) * (subW + MARGIN)
            if i == 0 ,user.openmoney == nil {
                subMember.backgroundColor = color_backround
                subMember.uiborderColor = color_border.cgColor
            }
            if let money = user.openmoney, money.elementsEqual(moneys[i]) {
                subMember.backgroundColor = color_backround
                subMember.uiborderColor = color_border.cgColor
            }
            subMember.frame = CGRect(x: subX, y: line2.frame.maxY + MARGIN25, width: subW, height: MARGIN140)
            subMember.resetTip = tips[i]
            subMember.resetMoney = moneys[i]
            scrollview.addSubview(subMember)
            // view的高度
            tempHeight = subMember.frame.maxY
            // 添加点击事件
            subMember.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(clickItem(ui:))))
        }
        // 自动续费
        let autoStr = "P.S：会员账号内的乘机人也可享受会员同等待遇。"
        let autoW = autoStr.boundingRect(w: frame.width, h: MARGIN20, fontSize: 13).width
        let auto = lables[4]
        auto.text = autoStr
        auto.textColor = COLOR_NICE
        auto.font = UIFont.systemFont(ofSize: 13)
        auto.frame = CGRect(x: MARGIN, y: tempHeight + MARGIN20, width: autoW, height: MARGIN20)
        scrollview.addSubview(auto)
        // 立即开通
        let saveBtnY = auto.frame.maxY + MARGIN30
        saveBtn.frame = CGRect(x: MARGIN, y: saveBtnY, width: SCREEN_WIDTH - MARGIN * 2, height: MARGIN50)
        scrollview.addSubview(saveBtn)
        saveBtn.addTarget(self, action: #selector(clickOpenBtn(btn:)), for: .touchUpInside)
        // 温馨提示
        let hotTip = lables[5]
        hotTip.text = "温馨提示:"
        hotTip.textColor = COLOR_NICE
        hotTip.font = UIFont.systemFont(ofSize: 17)
        hotTip.frame = CGRect(x: MARGIN, y: saveBtn.frame.maxY + MARGIN30, width: MARGIN100, height: MARGIN20)
        scrollview.addSubview(hotTip)
        // 问题1
        let question1 = lables[6]
        question1.text = "Q：会员过期有提示吗？"
        question1.textColor = UIColor.gray
        question1.font = UIFont.systemFont(ofSize: 13)
        question1.frame = CGRect(x: MARGIN, y: hotTip.frame.maxY + MARGIN, width: tipW, height: MARGIN)
        scrollview.addSubview(question1)
        // 答复1
        let answer1 = lables[7]
        answer1.text = "A：会员徽章显示为蓝色，以及个人资料显示会员过期。"
        answer1.textColor = UIColor.gray
        answer1.font = UIFont.systemFont(ofSize: 13)
        answer1.frame = CGRect(x: MARGIN, y: question1.frame.maxY + MARGIN10, width: tipW, height: MARGIN)
        scrollview.addSubview(answer1)
        // 问题2
        let question2 = lables[8]
        question2.text = "Q：换设备是否需要重新开通会员？"
        question2.textColor = UIColor.gray
        question2.font = UIFont.systemFont(ofSize: 13)
        question2.frame = CGRect(x: MARGIN, y: answer1.frame.maxY + MARGIN, width: tipW, height: MARGIN)
        scrollview.addSubview(question2)
        // 答复2
        let answer2Str = "A：不需要。会员与用户账号绑定，只要会员不过期，就不需要重新开通。"
        let answer2 = lables[9]
        answer2.text = answer2Str
        answer2.textColor = UIColor.gray
        answer2.font = UIFont.systemFont(ofSize: 13)
        answer2.frame = CGRect(x: MARGIN, y: question2.frame.maxY, width: tipW, height: MARGIN60)
        answer2.numberOfLines = 0
        answer2.attributedText = NSAttributedString(string: answer2Str, attributes: [.paragraphStyle : style])
        scrollview.addSubview(answer2)
        // 全部添加
        addSubview(scrollview)
        // 设置scrollview属性
        scrollview.showsVerticalScrollIndicator = false
        scrollview.frame = CGRect(x: 0, y: 0, width: frame.width, height: frame.height)
        scrollview.contentSize = CGSize(width: frame.width, height: answer2.frame.maxY + MARGIN40)
    }
    
    @objc func clickOpenBtn(btn:UIButton) -> Void {
        if let callback = complete {
            callback(moneys[moneyIndex],btn.currentTitle!)
        }
    }
    
    @objc func clickItem(ui:UITapGestureRecognizer) -> Void {
        // 恢复原状
        for subitem in subviews {
            for item in subitem.subviews {
                if item.isKind(of: SubMemberView.self) {
                    let subUi = item as! SubMemberView
                    subUi.backgroundColor = UIColor.white
                    subUi.uiborderColor = UIColor.gray.cgColor
                }
            }
        }
        // 点击选中
        let subUi = ui.view as! SubMemberView
        subUi.backgroundColor = color_backround
        subUi.uiborderColor = color_border.cgColor
        // 选中的金额
        moneyIndex = subUi.tag
    }
    
    class SubMemberView: UIView {
        
        // 文本
        private lazy var tip = UILabel(title: "", color: COLOR_TIP, size: 15, align: .center)
        // 金额
        private lazy var money = UILabel(title: "", color: COLOR_TIP)
        
        public var uiborderColor:CGColor? {
            didSet {
                setNeedsLayout()
            }
        }
        
        public var resetTip:String? {
            didSet {
                tip.text = resetTip
            }
        }
        
        public var resetMoney:String? {
            didSet {
                money.text = resetMoney
            }
        }
        
        override func layoutSubviews() {
            let ui = UIView(frame: CGRect(x: 0, y: 0, width: frame.width, height: frame.height))
            ui.layer.borderWidth = 2
            ui.layer.cornerRadius = 8
            ui.layer.borderColor = uiborderColor ?? UIColor.gray.cgColor
            // 文本
            tip.frame = CGRect(x: 0, y: MARGIN25, width: frame.width, height: MARGIN20)
            ui.addSubview(tip)
            // 符号
            let symbol = UILabel(title: "¥", color: COLOR_TIP, size: 14)
            let symbolX = (frame.width - MARGIN40) * 0.5 - MARGIN15
            symbol.frame = CGRect(x: symbolX, y: tip.frame.maxY + MARGIN25, width: MARGIN10, height: MARGIN40)
            ui.addSubview(symbol)
            // 金额
            money.font = UIFont.boldSystemFont(ofSize: 30)
            let moneY = (frame.height - MARGIN20) * 0.5 + MARGIN5
            money.frame = CGRect(x: symbol.frame.maxX, y: moneY, width: MARGIN60, height: MARGIN40)
            ui.addSubview(money)
            // 添加全部
            addSubview(ui)
        }
    }
}
